"""
@Project:p8project
@File:coures.py
@Author:袁浩天
@Date:11:51
"""
from flask_restful import Resource,Api,marshal
from flask import Blueprint,jsonify
from common.coures_fileds.course import *
from common.coures_fileds.user_fields import *
from common.models.coures import *
from flask_restful import reqparse
import json
from sqlalchemy import and_


course_bp = Blueprint("course_bp",__name__,url_prefix='/course')
api = Api(course_bp)


class CourseResource(Resource):
    def get(self):
        try:
            tags = CourseTagModel.query.all()
            data = marshal(tags, tag_fields)
            return jsonify(msg="标签获取成功", data=data)
        except Exception as e:
            print(e)
            return jsonify(msg="标签获取失败", code=500)

class CourseRes(Resource):
    def get(self):
        """获取所有的课程"""
        parser = reqparse.RequestParser()
        parser.add_argument("sort")
        parser.add_argument("tag_id")
        parser.add_argument("free")
        parser.add_argument("online")
        args = parser.parse_args()
        tag_id = args.get('tag_id')
        free = args.get('free')
        online = args.get('online')
        sort = args.get('sort')

        print("接收<<<<<<<", tag_id, free, online, sort)

        if tag_id:
            # 查询中间表里面的内容,以此来进行判断
            tag = CourseAndTagModel.query.filter_by(id=int(tag_id)).all()
            tag_marshal = marshal(tag, tag_fields)
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>tag",tag_marshal)

            # 设置一个空列表用于接受判定之后的内容
            tag_list = []

            # 循环中间表的查询内容
            for tag_data in tag:

                # 在课程表中根据中间表的id值查询
                course = CourseModel.query.filter_by(id=tag_data.course_id).first()
                print(">>>>>>>>>>>>>>>>>>>>>>>>.course", course)

                # 判断free条件是否相等
                if tag_data.course_id != free and free == None:
                    # continue跳出本次循环开始下一次
                    continue

                if course.status != online and online == None:
                    continue

                # 将课程表中的内容进行查询并转换格式
                course_fieldss = marshal(course, course_fields)
                # 将其转换为json格式用户判定
                course_json = json.loads(json.dumps(course_fieldss))
                print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.course>>>>json", course_json)

                # 根据课程进行查询以查找出所需内容
                type = CourseTypeModel.query.filter_by(id=course_json["course_type"]).first()
                # 添加name
                course_json["name"] = type.name

                # 添加到列表中append将其内容添加到指定列表的尾部
                tag_list.append(course_json)
            return jsonify(msg="获取成功", data=tag_list)
        else:
            tag = CourseAndTagModel.query.all()
            tag_marshal = marshal(tag,tag_fields)
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>tag",tag_marshal)
            tag_list = []
            for tag_data in tag:
                course = CourseModel.query.filter_by(id=tag_data.course_id).first()
                print(">>>>>>>>>>>>>>>>>>>>>>>>.course",course)
                if tag_data.course_id != free and free == None:
                    continue
                if course.status != online and online == None:
                    continue
                course_fieldss = marshal(course,course_fields)
                course_json = json.loads(json.dumps(course_fieldss))
                print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.course>>>>json",course_json)
                type = CourseModel.query.filter_by(id=course_json["course_type"]).first()
                course_json["name"] = type.name
                tag_list.append(course_json)
            return jsonify(msg="获取成功",data=tag_list)



class GetCourseInfo(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('cid')
        args = parser.parse_args()
        cid = args.get("cid")

        # 根据uid获取课程表数据
        get_course = CourseModel.query.filter_by(id=cid).first()
        # 有序字典转换成dict
        dict = marshal(get_course, course_fields)
        course_ser = json.loads(json.dumps(dict))

        chapters = ChapterModel.query.filter_by(course_type=get_course.id).all()

        order_dict_chapter = marshal(chapters, chapter_fields)

        chapters_dict = json.loads(json.dumps(order_dict_chapter))
        chapters_ser = []
        for i in chapters_dict:
            sections = SectionsModel.query.filter_by(chapter=i["id"]).all()
            sections_dict = json.loads(json.dumps(marshal(sections, section_fields)))
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>sections_dict", sections_dict)
            i["section"] = sections_dict
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>I",i)
            chapters_ser.append(i)
        data_ = {
            'course_ser': course_ser,
            'chapters_ser' : chapters_ser
        }
        print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", data_)

        return jsonify(data=data_, code=200, massage="获取成功")



class SectionvideoGet(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('sid')
        args = parser.parse_args()
        sid = args.get("sid")

        sections = SectionsModel.query.filter_by(chapter=sid).first()
        url = sections.video_url
        print("url>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.",url)
        return jsonify(message="获取成功", url="http://re47j5nx5.hn-bkt.clouddn.com/60e8d36eec3d99cd167aab6ad6cf74b1.mp4")



api.add_resource(CourseResource,'/tag')
api.add_resource(CourseRes,'/get_course')
api.add_resource(GetCourseInfo,'/courseinfo')
api.add_resource(SectionvideoGet, "/sectionvideo")

